package com.example.springbootmybatis.utility;

import com.example.springbootmybatis.entity.RestResult;

/**
 * @title: ResultUtil
 * @Author BanYunjian
 * @Date: 2021/9/5 14:49
 * @Version 1.0
 */
public class ResultUtil {
    /**
     * 封装请求成功时，返回的数据
     *
     * @param rspCode 反应代码
     * @param data    返回的数据
     * @param message 描述
     * @param <T>
     * @return
     */
    private static <T> RestResult<T> genResult(String rspCode, T data, String message) {
        RestResult<T> result = RestResult.newInstance();
        result.setRspCode(rspCode);
        result.setData(data);
        result.setRspDesc(message);
        return result;
    }

    /**
     * 封装请求失败时，返回的数据
     *
     * @param rspCode 反应代码
     * @param message 描述
     * @param <T>
     * @return
     */
    private static <T> RestResult<T> genErrorResult(String rspCode, String message) {
        RestResult<T> result = RestResult.newInstance();
        result.setRspCode(rspCode);
        result.setRspDesc(message);
        return result;
    }

    /**
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> RestResult<T> genSuccessResult(T data){
        return genResult("000", data, "请求成功");
    }

    /**
     *
     * @param rspCode
     * @param <T>
     * @return
     */
    public static <T> RestResult<T> genErrorResult(String rspCode){
        return genErrorResult(rspCode, "请求失败");
    }
}
